package com.art.sunflower.controller;

import com.art.sunflower.exception.UsernameExistException;
import com.art.sunflower.pojo.Login;
import com.art.sunflower.pojo.User;
import com.art.sunflower.service.UserService;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

@RestController
@CrossOrigin
@RequestMapping(value = "/user")
@Api(value = "用户控制器")
public class UserController {

    @Resource
    UserService userService;

    @ApiOperation(value = "普通登录")
    @PostMapping(value = "login")
    public Message login(@RequestBody Login login) {
        String msg;
        try {
            return Message.createSuc(userService.login(login.getUsername(), login.getPassword()));
        } catch (DisabledAccountException e) {
            e.printStackTrace();
            msg = "账号已被禁用";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            msg = "账号不存在";
        } catch (ExcessiveAttemptsException e) {
            e.printStackTrace();
            msg = "登录失败次数过多";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            msg = "账号或密码输入错误";
        } catch (ExpiredCredentialsException e) {
            msg = "密码失效";
        }
        return Message.createErr(msg);
    }

    @ApiOperation(value = "注册(暂时用作md5加密)")
    @PostMapping(value = "register")
    public Message register(@RequestBody User user) {
        String msg = "注册成功，请前往登录界面登录";
        try {
            userService.register(user);
            return Message.createSuc(msg);
        } catch (UsernameExistException e) {
            e.printStackTrace();
            msg = "用户名已存在";
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            msg = "注册失败";
        }
        return Message.createErr(msg);
    }

    @ApiOperation(value = "重置密码")
    @PutMapping(value = "passwordReset")
    public Message resetPassword(String oldOne, String newOne) {
        String msg = "密码已修改，请重新登录";
        try {
            userService.resetPassword(oldOne, newOne);
            return Message.createSuc(msg);
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            msg = "原密码错误";
        }
        return Message.createErr(msg);
    }

    @ApiOperation(value = "登出")
    @GetMapping(value = "logout")
    public Message logout() {
        String msg = "退出成功";
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.logout();
            return Message.createSuc(msg);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            msg = "请先登录！";
        }
        return Message.createErr(msg);
    }

    @ApiOperation(value = "修改用户信息")
    @PutMapping()
    public Message updateInformation(String realName, String email) {
        userService.updateInformation(realName, email);
        return Message.createSuc("修改成功");
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping()
    public Message getInformation() {
        return Message.createSuc(userService.getInformation());
    }

}
